﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace RMD.quickswitch
{
    public struct WindowDesc
    {
        public WindowDesc(IntPtr hwnd, string title)
        {
            this.m_hWnd = hwnd;
            this.m_title = title;
        }
        public string m_title;
        public IntPtr m_hWnd;
    }

    public class WindowManager
    {

        private List<WindowDesc> m_windows = new List<WindowDesc>();

        public List<WindowDesc> WindowList
        {
            get { return m_windows; }
        }

        public void RefreshWindowList()
        {
            m_windows.Clear();
            Win32.EnumWindows(new Win32.EnumWindowsProc(OnEnumWindow), IntPtr.Zero);
        }

        bool OnEnumWindow(IntPtr hWnd, IntPtr lParam)
        {
            Win32.WINDOWINFO info = new Win32.WINDOWINFO();
            info.cbSize = (uint)Marshal.SizeOf(info);
            Win32.GetWindowInfo(hWnd, ref info);
            if ((info.dwStyle & (uint)Win32.WIN_STYLE.WS_VISIBLE) == 0)
                return true;
            StringBuilder sb = new StringBuilder(1024);
            Win32.GetWindowText(hWnd, sb, 1024);
            if (sb.Length > 0)
                m_windows.Add(new WindowDesc(hWnd, sb.ToString()));
            return true;
        }

        public void ActiveWindow(IntPtr hWnd)
        {
            Win32.SetForegroundWindow(hWnd);
        }

    }
}
